home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / helpcpp.exe / HELP.CPP < prev    next >
C/C++ Source or Header  |  1992-07-10  |  11KB  |  402 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       Help.cpp                                  */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  Member function(s) of following classes   */
  6. /*                      THelpViewer                           */
  7. /*                      THelpWindow                           */
  8. /*------------------------------------------------------------*/
  9.  
  10. /*------------------------------------------------------------*/
  11. /*                                                            */
  12. /*    Turbo Vision -  Version 1.0                             */
  13. /*                                                            */
  14. /*                                                            */
  15. /*    Copyright (c) 1991 by Borland International             */
  16. /*    All Rights Reserved.                                    */
  17. /*                                                            */
  18. /*------------------------------------------------------------*/
  19.  
  20. #define Uses_TStreamableClass
  21. #define Uses_TPoint
  22. #define Uses_TStreamable
  23. #define Uses_ipstream
  24. #define Uses_opstream
  25. #define Uses_fpstream
  26. #define Uses_TRect
  27. #define Uses_TScrollBar
  28. #define Uses_TScroller
  29. #define Uses_TDrawBuffer
  30. #define Uses_TEvent
  31. #define Uses_TWindow
  32. #define Uses_TKeys
  33. #define Uses_TPalette
  34. #include <tv.h>
  35.  
  36. #if !defined( __HELP_H )
  37. #include "Help.h"
  38. #endif  // __HELP_H
  39.  
  40. #if !defined( __UTIL_H )
  41. #include "Util.h"
  42. #endif  // __UTIL_H
  43.  
  44. #if !defined( __STRING_H )
  45. #include <string.h>
  46. #endif  // __STRING_H
  47.  
  48. #if !defined( __LIMITS_H )
  49. #include <limits.h>
  50. #endif  // __LIMITS_H
  51.  
  52. #if !defined( __STAT_H )
  53. #include <sys\stat.h>
  54. #endif  // __STAT_H
  55.  
  56. #if !defined( __CTYPE_H )
  57. #include <ctype.h>
  58. #endif  // __CTYPE_H
  59.  
  60. #if !defined( __IO_H )
  61. #include <io.h>
  62. #endif  // __IO_H
  63.  
  64. #pragma warn -dsz
  65.  
  66.  
  67. /**************************************************************************
  68.                          C L A S S   S T A C K 
  69. **************************************************************************/
  70. #define STK_SIZE    10
  71. #define STK_SHIFT    5
  72.  
  73. class Stack
  74. {
  75.   int stk[STK_SIZE];
  76.   int current;
  77.   int p;
  78. public:
  79.   Stack() {}
  80.   void reset();
  81.   void save(int v);
  82.   void push(int v);
  83.   int pop();
  84. };
  85.  
  86. Stack stack;
  87.  
  88. /**************************************************************************
  89.                        S T A C K : : R E S E T   
  90. **************************************************************************/
  91.  
  92. void Stack::reset()
  93. {
  94.     p = 1;
  95.     stk[0] = 0;
  96.     current = 0;
  97. } /* end Stack::Stack */
  98.  
  99.  
  100. /**************************************************************************
  101.                          S T A C K : : P U S H 
  102. **************************************************************************/
  103.  
  104. void Stack::push(int v)
  105. {
  106.   if (p >= STK_SIZE)
  107.   {
  108.     // Shift values to the left by STK_SHIFT, saving the first
  109.     memmove(&stk[1],&stk[1+STK_SHIFT],(STK_SIZE-STK_SHIFT-1)*sizeof(int));
  110.     p -= STK_SHIFT;
  111.   }
  112.   if (p > 0)
  113.     stk[p++] = current;
  114.   else if (current != stk[0])
  115.   {
  116.     stk[++p] = current;
  117.     p++;
  118.   }
  119.   current = v;
  120. } /* end Stack::push */
  121.  
  122.  
  123. /**************************************************************************
  124.                          S T A C K : : S A V E 
  125. **************************************************************************/
  126.  
  127. void Stack::save(int v)
  128. {
  129.   current = v;
  130. } /* end Stack::save */
  131.  
  132.  
  133. /**************************************************************************
  134.                           S T A C K : : P O P 
  135. **************************************************************************/
  136.  
  137. int Stack::pop()
  138. {
  139.   if (p > 0)
  140.     p--;
  141.   return stk[p];
  142. } /* end Stack::pop */
  143.  
  144. // THelpViewer
  145.  
  146. THelpViewer::THelpViewer( const TRect& bounds, TScrollBar* aHScrollBar,
  147.     TScrollBar* aVScrollBar, THelpFile *aHelpFile, ushort context )
  148.     : TScroller( bounds, aHScrollBar, aVScrollBar )
  149. {
  150.     options = (options | ofSelectable);
  151.     growMode = gfGrowHiX | gfGrowHiY;
  152.     hFile = aHelpFile;
  153.     topic = aHelpFile->getTopic(context);
  154.     topic->setWidth(size.x);
  155.     setLimit(78, topic->numLines());
  156.     selected = 1;
  157.     stack.reset();
  158.     stack.save(context);
  159. }
  160.  
  161. THelpViewer::~THelpViewer()
  162. {
  163.     delete hFile;
  164.     delete topic;
  165. }
  166.  
  167. void THelpViewer::changeBounds( TRect& bounds )
  168. {
  169.     TScroller::changeBounds(bounds);
  170.     topic->setWidth(size.x);
  171.     setLimit(limit.x, topic->numLines());
  172. }
  173.  
  174. void THelpViewer::draw()
  175. {
  176.     TDrawBuffer b;
  177.     char line[256];
  178.     char buffer[256];
  179.     char *bufPtr;
  180.     int i, j, l;
  181.     int keyCount;
  182.     ushort normal, keyword, selKeyword, c;
  183.     TPoint keyPoint;
  184.     uchar keyLength;
  185.     int keyRef;
  186.  
  187.     normal = getColor(1);
  188.     keyword = getColor(2);
  189.     selKeyword = getColor(3);
  190.     keyCount = 0;
  191.     keyPoint.x = 0;
  192.     keyPoint.y = 0;
  193.     topic->setWidth(size.x);
  194.     if (topic->getNumCrossRefs() > 0)
  195.         {
  196.         do
  197.             {
  198.             ++keyCount;
  199.             topic->getCrossRef(keyCount-1, keyPoint, keyLength, keyRef);
  200.             } while ( (keyCount <= topic->getNumCrossRefs()) && (keyPoint.y < delta.y));
  201.         }
  202.     for (i = 1; i <= size.y; ++i)
  203.         {
  204.         b.moveChar(0, ' ', normal, size.x);
  205.         strcpy(line, topic->getLine(i + delta.y));
  206.         if (strlen(line) > delta.x)
  207.             {
  208.             bufPtr = line + delta.x; 
  209.             strncpy(buffer, bufPtr, size.x);
  210.             buffer[size.x] = 0;
  211.             b.moveStr(0, buffer, normal);
  212.             }
  213.         else
  214.             b.moveStr(0, "", normal);
  215.         while (i + delta.y == keyPoint.y)
  216.             {
  217.             l = keyLength;
  218.             if (keyPoint.x < delta.x )
  219.                 {
  220.                 l -= (delta.x - keyPoint.x);
  221.                 keyPoint.x = delta.x;
  222.                 }
  223.             if (keyCount == selected)
  224.                 c = selKeyword;
  225.             else
  226.                 c = keyword;
  227.             for(j = 0; j < l; ++j)
  228.                 b.putAttribute((keyPoint.x - delta.x + j),c);
  229.             ++keyCount;
  230.             if (keyCount <= topic->getNumCrossRefs())
  231.                 topic->getCrossRef(keyCount-1, keyPoint, keyLength, keyRef);
  232.             else
  233.                 keyPoint.y = 0;
  234.             }
  235.         writeLine(0, i-1, size.x, 1, b);
  236.         }
  237. }
  238.  
  239. TPalette& THelpViewer::getPalette() const
  240. {
  241.     static TPalette palette(cHelpViewer, sizeof( cHelpViewer)-1);
  242.     return palette;
  243. }
  244.  
  245. void THelpViewer::makeSelectVisible( int selected, TPoint& keyPoint,
  246.          uchar& keyLength, int& keyRef )
  247. {
  248.     TPoint d;
  249.  
  250.     topic->getCrossRef(selected, keyPoint, keyLength, keyRef);
  251.     d = delta;
  252.     if (keyPoint.x < d.x)
  253.         d.x = keyPoint.x;
  254.     if (keyPoint.x > d.x + size.x)
  255.         d.x = keyPoint.x - size.x;
  256.     if (keyPoint.y < d.y)
  257.         d.y = keyPoint.y;
  258.     if (keyPoint.y > d.y + size.y)
  259.         d.y = keyPoint.y - size.y;
  260.     if ((d.x != delta.x) || (d.y != delta.y))
  261.          scrollTo(d.x, d.y);
  262. }
  263.  
  264. void THelpViewer::switchToTopic( int keyRef )
  265. {
  266.     if (topic != 0)
  267.         delete topic;
  268.     topic = hFile->getTopic(keyRef);
  269.     topic->setWidth(size.x);
  270.     scrollTo(0, 0);
  271.     setLimit(limit.x, topic->numLines());
  272.     selected = 1;
  273.     drawView();
  274. }
  275.  
  276. void THelpViewer::handleEvent( TEvent& event )
  277. {
  278.  
  279.     TPoint keyPoint, mouse;
  280.     uchar keyLength;
  281.     int keyRef;
  282.     int keyCount;
  283.  
  284.  
  285.     TScroller::handleEvent(event);
  286.     switch (event.what)
  287.         {
  288.         case evKeyDown:
  289.             switch (event.keyDown.keyCode)
  290.                 {
  291.                 case kbTab:
  292.                     ++selected;
  293.                     if (selected > topic->getNumCrossRefs())
  294.                         selected = 1;
  295.                     if ( topic->getNumCrossRefs() != 0 )
  296.                         makeSelectVisible(selected-1,keyPoint,keyLength,keyRef);
  297.                     break;
  298.  
  299.                 case kbShiftTab:
  300.                     --selected;
  301.                     if (selec